package besuper;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

public class BeSuperApp {
	private final String LIST = "list";
	private final String SCORE = "score";
	private final String QUESTION = "ask";
	private final String ANSWER = "answer";
	private final String HELP = "help";
	private final String EXIT = "exit";

	private String command;
	private BeSuper besuper=null;
	private User user=null;

	public static void main(String[] args) {
		BeSuperApp bs = new BeSuperApp();
		
		bs.start();
		
//		bs.load();
	}

	public void load() {
		if (!(new File("besuper.xml").exists())) {
			besuper = new BeSuper();
			marshal(besuper);
		} else {
			besuper = unmarshal();
		}
	}

	public static void marshal(BeSuper besuper) {
		try {
			JAXBContext ctx = JAXBContext.newInstance(BeSuper.class);
			ctx.createMarshaller().marshal(besuper, new File("besuper.xml"));
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}

	public static BeSuper unmarshal() {
		BeSuper besuper = null;
		try {
			JAXBContext ctx = JAXBContext.newInstance(BeSuper.class);
		    besuper = (BeSuper)ctx.createUnmarshaller().unmarshal(new File("besuper.xml"));
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return besuper;
	}

	public void init() {

		load();

		String name = BeSuperConsole.prompt("Welcome to there,please enter your name:");
		for (User us : besuper.getUsers()) {
			if (name.equals(us.getName())) {
				user = us;
				break;
			}
		}
		if (user == null) {
			user = new User();
			user.setName(name);
			user.setScore(0);
			besuper.getUsers().add(user);
		}
	}

	public void start() {
		init();
		BeSuperConsole.println("You can input 'help' for command ");
		while (true) {
			command = BeSuperConsole.prompt("cmd>");
			if (LIST.equalsIgnoreCase(command)) {
				listCommand();
			} else if (SCORE.equalsIgnoreCase(command)) {
				scoreCommand();
			} else if (QUESTION.equalsIgnoreCase(command)) {
				questionCommand();
			} else if (ANSWER.equalsIgnoreCase(command)) {
				answerCommand();
			} else if (HELP.equalsIgnoreCase(command)) {
				helpCommand();
			} else if (EXIT.equalsIgnoreCase(command)) {
				exitCommand();
				break;
			} else {
				errCommand();
			}
		}
	}

	public void errCommand() {
		BeSuperConsole.println("you can input 'help' for command");
	}

	public void exitCommand() {
		BeSuperConsole.println("bye  bye");

	}

	public void helpCommand() {
		BeSuperConsole.println("help     list the command");
		BeSuperConsole.println("list     list the question");
		BeSuperConsole.println("ask      create the question");
		BeSuperConsole.println("answer   select a question to answer");
		BeSuperConsole.println("score    show your score");
		BeSuperConsole.println("exit     quit the BeSuper");

	}

	public void answerCommand() {
		String id = BeSuperConsole.prompt("Input the question id:");

		Question q = null;
		Answer a = null;
		for (Question que : besuper.getQuestions()) {
			if (id.equals(que.getId())) {
				q = que;
				break;
			}
		}
		if (q == null) {
			BeSuperConsole.println("Question for id " + id + "is not exit");
		} else if (q.isSloved()) {
			for (Answer an : besuper.getAnswers()) {
				if (id.equalsIgnoreCase(an.getId())) {
					a = an;
					break;
				}
			}
			BeSuperConsole.println("The question is solved,chose another");
		}
		else{
		
		String context = BeSuperConsole.prompt("Input your answer ");

		a = new Answer();
		a.setId(id);
		a.setFrom(user.getName());
		a.setContext(context);

		q.setSolved(true);
		user.setScore(user.getScore() + 10);
		besuper.getAnswers().add(a);
		marshal(besuper);
		BeSuperConsole.println("Thank for your answer");
		}
	}

	public void questionCommand() {
		String context = BeSuperConsole.prompt("Input your question");

		Question que = new Question();
		que.setContext(context);
		que.setId(System.currentTimeMillis() + "");
		que.setFrom(user.getName());
		que.setSolved(false);

		besuper.getQuestions().add(que);
		marshal(besuper);
		BeSuperConsole.println("you create a question ");

	}

	public void scoreCommand() {
		BeSuperConsole.println("your score :" + user.getScore());
	}

	public void listCommand() {
		for (Question que : besuper.getQuestions()) {
			BeSuperConsole.println(que.toString());
		}
	}

}
